package com.violet.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.violet.domain.po.BuyOrder;
import com.violet.domain.po.Product;
import com.violet.domain.po.Rider;
import com.violet.domain.po.User;
import com.violet.domain.search.manager.MOrderSearch;
import com.violet.domain.search.manager.MProductSearch;
import com.violet.domain.search.manager.MRiderSearch;
import com.violet.domain.search.manager.MUserSearch;

public class SearchUtils {

    public static LambdaQueryWrapper<Product> getSearchWrapper(MProductSearch search) {
        return new LambdaQueryWrapper<Product>()
                .like(search.getName() != null && !search.getName().trim().isEmpty(), Product::getName, search.getName())
                .eq(search.getCategoryId() != null && !search.getCategoryId().trim().isEmpty(), Product::getCategoryId, search.getCategoryId())
                .eq(search.getStatus() != null, Product::getStatus, search.getStatus())
                .ge(search.getMinPrice() != null, Product::getCurrentPrice, search.getMinPrice())
                .le(search.getMaxPrice() != null, Product::getCurrentPrice, search.getMaxPrice());
    }

    public static LambdaQueryWrapper<Rider> getSearchWrapper(MRiderSearch search) {
        return new LambdaQueryWrapper<Rider>()
            .like(search.getName() != null && !search.getName().trim().isEmpty(), Rider::getName, search.getName())
            .or(search.getPhone() != null && !search.getPhone().trim().isEmpty())
            .eq(search.getPhone() != null && !search.getPhone().trim().isEmpty(), Rider::getPhone, search.getPhone())
            .eq(search.getStatus() != null, Rider::getStatus, search.getStatus());
    }

    public static LambdaQueryWrapper<BuyOrder> getSearchWrapper(MOrderSearch search) {
        return new LambdaQueryWrapper<BuyOrder>()
                .eq(search.getId() != null && !search.getId().trim().isEmpty(), BuyOrder::getId, search.getId())
                .eq(search.getUserId() != null && !search.getUserId().trim().isEmpty(), BuyOrder::getUserId, search.getUserId())
                .like(search.getStatus() != null, BuyOrder::getStatus, search.getStatus())
                .and(search.getDateRange() != null, wrapper -> wrapper.between(
                        BuyOrder::getOrderTime, search.getDateRange().get(0), search.getDateRange().get(1)
                ));
    }

    public static LambdaQueryWrapper<User> getSearchWrapper(MUserSearch search) {
        return new LambdaQueryWrapper<User>()
                .like(search.getUsername() != null && !search.getUsername().trim().isEmpty(), User::getUsername, search.getUsername())
                .like(search.getPhone() != null && !search.getPhone().trim().isEmpty(), User::getPhone, search.getPhone())
                .eq(search.getStatus() != null, User::getStatus, search.getStatus());
    }
}
